package com.example.administrator.liveapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.FoundBean;

import java.util.List;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class FoundFragmentAdapter extends RecyclerView.Adapter<FoundFragmentAdapter.ViewHolder> implements View.OnClickListener {

    public List<FoundBean.ArticleBean> article;
    public FoundFragmentAdapter(List<FoundBean.ArticleBean> article){
        this.article = article;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(article!=null&&article.size()>0){
            holder.article_item_text.setText(article.get(position).getArticletitle());
            holder.article_author_name.setText("作者："+article.get(position).getConsumername());
            holder.article_Thumb_up_number.setText(article.get(position).getArticleviewcount()+"人");
            holder.article_watch_number.setText(article.get(position).getArticlebestcount()+"人");
            //将position保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        return article.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView article_item_text;
        public TextView article_author_name;
        public TextView article_watch_number;
        public TextView article_Thumb_up_number;
        public ViewHolder(View itemView) {
            super(itemView);
            article_item_text = (TextView) itemView.findViewById(R.id.article_item_text);
            article_author_name = (TextView) itemView.findViewById(R.id.article_author_name);
            article_watch_number = (TextView) itemView.findViewById(R.id.article_watch_number);
            article_Thumb_up_number = (TextView) itemView.findViewById(R.id.article_Thumb_up_number);
        }
    }

    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
